package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.AreaInfoQry;
import com.jftone.bh.app.model.NtbBase;
import com.jftone.bh.app.model.NtbRecord;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.util.BusiNoUtil;
import com.jftone.bh.app.util.DataUtil;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.List;


@Service
public class UserService {
	
	private Logger logger = Logger.getLogger(UserService.class);
	
	@Autowired
	private Dao dao;

	public List<IData<String, Object>> queryList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> list = null;
		String account = data.getString("account", "");
		String userName = data.getString("userName", "");
		String uplevel = data.getString("uplevel", "");
		String hphm = data.getString("hphm", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(account)){
			param.put("ACCOUNT","%"+account+"%");
		}
		if(!StringUtil.isBlank(hphm)){
			param.put("HPHM","%"+hphm+"%");
		}
		if(!StringUtil.isBlank(userName)){
			param.put("USER_NAME","%"+userName+"%");
		}
		if(!StringUtil.isBlank(uplevel)){
			param.put("UPLEVEL", uplevel);
		}
		list = dao.query("queryUserList", param, page);
		NtbUser ntbUser;
		NtbBase base;
		for (IData<String, Object> user : list) {
			if(!StringUtil.isEmpty(user.getString("PID"))){
				ntbUser = getNtbUser(user.getInt("PID"));
				if(ntbUser != null){
					user.put("P_ACCOUNT",ntbUser.getAccount());
				}
			}
			user.put("AFFILIATION_NAME", "");
			if(!StringUtil.isEmpty(user.getString("AFFILIATION",""))){
				short uplevel_ = user.getShort("UPLEVEL",SysConst.VIP_NO);
				if(uplevel_ == SysConst.VIP_SUB){
					param.clear();
					param.put("id",user.getInt("AFFILIATION",0));
					base = dao.get(NtbBase.class,param);
					if(base != null){
						user.put("AFFILIATION_NAME", base.getBname());
					}
				}else if(uplevel_ == SysConst.VIP_SUB_SUB){
					user.put("AFFILIATION_NAME", AreaInfoQry.getAreaName("0",user.getString("AFFILIATION","")));
				}
			}
		}
		return list;
	}
	@Transactional
	public int delete(long id ) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",id);
		NtbUser ntbUser =dao.get(NtbUser.class,param);
		if(ntbUser==null){
			return 1;
		}else{
			NtbUser ntbUserTemp = new NtbUser();
			ntbUserTemp.setId(ntbUser.getId());
			ntbUserTemp.setIsRemove(SysConst.REMOVE_TAG_YES);
			dao.update(ntbUserTemp);
		}
		return 0;
	}

	@Transactional
	public int regain(long id ) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",id);
		NtbUser ntbUser =dao.get(NtbUser.class,param);
		if(ntbUser==null){
			return 1;
		}else{
			NtbUser ntbUserTemp = new NtbUser();
			ntbUserTemp.setId(ntbUser.getId());
			ntbUserTemp.setIsRemove(SysConst.REMOVE_TAG_NO);
			dao.update(ntbUserTemp);
		}
		return 0;
	}

	@Transactional
	public int resetLoginPass(long id ) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",id);
		NtbUser ntbUser =dao.get(NtbUser.class,param);
		if(ntbUser==null){
			return 1;
		}else{
			NtbUser ntbUserTemp = new NtbUser();
			ntbUserTemp.setId(ntbUser.getId());
			ntbUserTemp.setFirstPwd(DataUtil.switchPwd("00000000"+DataUtil.MD_SIGN));
			dao.update(ntbUserTemp);
		}
		return 0;
	}

	public NtbUser getNtbUser(IData<String, Object> param) throws DaoException {
		return dao.get(NtbUser.class,param);
	}

	public NtbUser getNtbUser(int userId) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",userId);
		return getNtbUser(param);
	}

	@Transactional
	public synchronized int saveMoneyOper(IData<String, Object> data,String adminUser) throws DaoException {
		int id = data.getInt("dataId");
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",id);
		NtbUser ntbUser =dao.get(NtbUser.class,param);
		if(ntbUser==null){
			return 1;
		}
		short type = data.getShort("updateType");
		short currency = data.getShort("currencyType");
		int afterMoneyTemp = (int) (data.getDouble("afterMoney")*100);
		int updateMoney = (int) (data.getDouble("updateMoney")*100);
		int afterMoney = 0,oldMoney=0;
		if(currency == SysConst.RECORD_CURRENCY_1){
			oldMoney = ntbUser.getMlMoney();
		}else if(currency == SysConst.RECORD_CURRENCY_2){
			oldMoney = ntbUser.getStampMoney();
		}else{
			return 2;
		}
		if(type == SysConst.RECORD_TYPE_2 ){
			afterMoney = oldMoney +updateMoney;
		}else if(type == SysConst.RECORD_TYPE_3){
			afterMoney = oldMoney - updateMoney;
		}else{
			return 2;
		}
		if(afterMoney != afterMoneyTemp){
			return 3;
		}else if(afterMoney<0){
			return 4;
		}
		NtbUser ntbUserTemp = new NtbUser();
		int amount;
		if(currency == SysConst.RECORD_CURRENCY_1){
			ntbUserTemp.setMlMoney(afterMoney);
			amount = afterMoney - ntbUser.getMlMoney();
		}else{
			ntbUserTemp.setStampMoney(afterMoney);
			amount = afterMoney - ntbUser.getStampMoney();
		}
		ntbUserTemp.setId(id);
		dao.update(ntbUserTemp);

		NtbRecord ntbRecord = new NtbRecord();
		ntbRecord.setAddDate(DateUtil.getNowStr(DateUtil.FMT_DATETIME));
		ntbRecord.setAmount(amount);
		ntbRecord.setAfterAmount(afterMoney);
		ntbRecord.setConversionAmount(amount);
		ntbRecord.setBuyid("0");
		ntbRecord.setCurrency(currency);
		ntbRecord.setMtype(SysConst.RECORD_MTYPE_0);
		ntbRecord.setType(type);
		ntbRecord.setCfnumber(BusiNoUtil.getCFNo());
		ntbRecord.setStatus(SysConst.RECORD_STATUS_1);
		ntbRecord.setAccepter(id+"");
		ntbRecord.setOperation(adminUser);
		ntbRecord.setRemark(data.getString("remark"));
		dao.save(ntbRecord);
		return 0;
	}
}
